package tw.miku.handler;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

import flex.messaging.io.amf.ASObject;

public class ServerPolicyHandler extends IoHandlerAdapter
{

	@Override
	public void sessionClosed(IoSession session) throws Exception
	{
		super.sessionClosed(session);
	}

	@Override
	public void messageReceived(IoSession session, Object message)
			throws Exception
	{
		try
		{
			if (message instanceof ASObject)
			{
			}
			else
			{
				sendSecurity(session);
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	@Override
	public void exceptionCaught(IoSession session, Throwable cause)
			throws Exception
	{
		session.close(false);
	}

	private void sendSecurity(IoSession session)
	{
		String xml = "<cross-domain-policy>";
		xml = xml + "<allow-access-from domain=\"*\" to-ports=\"*\"/>";
		xml = xml + "</cross-domain-policy>";
		session.write(xml);
	}
}
